<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="465px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
        <el-col :span="24">
          <el-form-item label="生产日期">
            <el-date-picker
              v-model="daterangeMoeScrq"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              style="width: 250px"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机台编码" prop="moeJtbh">
            <el-input v-model="queryParams.moeJtbh" placeholder="请输入机台编码" style="width: 250px" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-form-item label="" prop="moeZtbz">
            <el-radio-group v-model="queryParams.moeZtbz">
              <el-radio label="BB">白班</el-radio>
              <el-radio label="YB">夜班</el-radio>
              <el-radio label="">白班夜班</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" @click="handleSearch" size="small">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport('#ComparativeAnalysis', '各班次效率对比分析-明细数据')"
          size="small">
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport('#ComparativeAnalysisStatistics', '各班次效率对比分析-统计信息')"
          size="small">
          统计信息
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: -10px; top: 0">
        <Page
          v-if="activeTab === 'first'"
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total1"
          @on-change="pageChange1"
          simple></Page>
        <Page
          v-if="activeTab === 'second'"
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total2"
          @on-change="pageChange2"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="activeTab" @tab-click="handleClick">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          id="ComparativeAnalysis"
          :height="tableH"
          :data="sddDetList"
          :loading="loading1"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" width="90" prop="sddThrq" />
          <el-table-column label="班次" align="center" prop="sddBcmc" width="90" />
          <el-table-column label="机台" align="center" prop="sddJtbh" width="90" />
          <el-table-column label="指令代码" align="center" prop="sddZldm" width="90" />
          <el-table-column label="指令名称" align="center" prop="sddZlmc" width="90" />
          <el-table-column label="用时(H)" align="center" prop="sddHour" width="90" />
          <el-table-column label="发生次数" align="center" prop="sddCnt" width="90" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="统计信息" name="second">
        <el-table
          border
          stripe
          id="ComparativeAnalysisStatistics"
          :height="tableH"
          :data="sddDetTotal"
          :loading="loading2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" width="90" prop="sddThrq" />
          <el-table-column label="班次" align="center" width="80" prop="sddBcmc" />
          <el-table-column label="机台" align="center" width="80" prop="sddJtbh" />
          <el-table-column label="计划数量" align="center" width="130" prop="sddJhsl" />
          <el-table-column label="完成数量" align="center" prop="sddSjsl" width="130" />
          <el-table-column label="计划达成率%" align="center" prop="sddRate" width="80" />
          <el-table-column label="机台移动率%" align="center" prop="sddYzrate" width="80" />
          <el-table-column label="换模(H)" align="center" prop="sdd50h" width="80" />
          <el-table-column label="次数" align="center" prop="sdd50cnt" width="90" />
          <el-table-column label="调机(H)" align="center" prop="sdd51h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd51cnt" width="90" />
          <el-table-column label="换料(H)" align="center" prop="sdd52h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd52cnt" width="90" />
          <el-table-column label="首件(H)" align="center" prop="sdd53h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd53cnt" width="90" />
          <el-table-column label="修模(H)" align="center" prop="sdd54h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd54cnt" width="90" />
          <el-table-column label="停机(H)" align="center" prop="sdd55h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd55cnt" width="90" />
          <el-table-column label="待料(H)" align="center" prop="sdd56h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd56cnt" width="90" />
          <el-table-column label="首件NG" align="center" prop="sdd57h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd57cnt" width="90" />
          <el-table-column label="试模(H)" align="center" prop="sdd58h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd58cnt" width="90" />
          <el-table-column label="保养(H)" align="center" prop="sdd59h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd59cnt" width="90" />
          <el-table-column label="异常停机" align="center" prop="sdd60h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd60cnt" width="90" />
          <el-table-column label="排程异常" align="center" prop="sdd61h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd61cnt" width="90" />
          <el-table-column label="正常生产" align="center" prop="sdd62h" width="90" />
          <el-table-column label="次数" align="center" prop="sdd62cnt" width="90" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {exportTable} from '@/utils/ruoyi'
import {listClassAnalyseDetail, listClassAnalyseTotal} from '@/api/production/classAnalyse'

export default {
  name: 'ClassAnalyse',
  data() {
    return {
      // 遮罩层
      loading1: true,
      loading2: false,
      tableH: document.body.clientHeight - 300,
      activeTab: 'first',
      sddDetList: [],
      sddDetTotal: [],
      // 显示搜索条件
      openSearch: false,
      showSearch: true,
      // 页数
      pageNum1: 1,
      pageNum2: 1,
      // 总条数
      total1: 0,
      total2: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层Rq
      open: false,
      // 建立日期时间范围
      daterangeMoeScrq: [
        this.$moment().startOf('month').toDate(),
        this.$moment().add(100, 'year').endOf('month').toDate()
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        moeKsscrq: null,
        moeJsscrq: null,
        moeJtbh: null,
        moeZtbz: '',
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading1 = true
      this.loading2 = false
      if (null != this.daterangeMoeScrq && '' != this.daterangeMoeScrq) {
        this.queryParams.moeKsscrq = this.moment(this.daterangeMoeScrq[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.moeJsscrq = this.moment(this.daterangeMoeScrq[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listClassAnalyseDetail(this.queryParams)
        .then((response) => {
          // 明细
          this.sddDetList = response.data.rows
          this.total1 = response.data.total
          this.loading1 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading1 = false
        }),
      listClassAnalyseTotal(this.queryParams)
        .then((response) => {
          // 统计
          this.sddDetTotal = response.data.rows
          this.total2 = response.data.total
          this.loading2 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading2 = false
        })
    },
    handleSearch() {
      this.openSearch = true
      this.title = '查询'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    // 表单重置
    reset() {
      this.form = {
        moeZzdh: null,
        moeWldm: null,
        moeJtbh: null,
      }
      this.resetForm('form')
    },
    handleClick(tab) {
      if (tab.name == 'first') {
        this.pageNum2 = this.queryParams.pageNum
        this.queryParams.pageNum = this.pageNum1
      } else if (tab.name == 'second') {
        this.pageNum1 = this.queryParams.pageNum
        this.queryParams.pageNum = this.pageNum2
      }
    },
    pageChange1(page) {
      this.loading1 = true
      this.queryParams.pageNum = page
      listClassAnalyseDetail(this.queryParams)
        .then((response) => {
          // 明细
          this.sddDetList = response.data.rows
          this.total1 = response.data.total
          this.loading1 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading1 = false
        })
    },
    pageChange2(page) {
      this.loading2 = true
      this.queryParams.pageNum = page
      listClassAnalyseTotal(this.queryParams)
        .then((response) => {
          // 统计
          this.sddDetTotal = response.data.rows
          this.total2 = response.data.total
          this.loading2 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading2 = false
        })
    },
    //导出Excel
    handleExport(a, b) {
      exportTable(a, b)
    },
  },
}
</script>

<style lang="scss" scoped></style>
